/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.persistence.entity.configuration;

import javax.persistence.*;
import java.util.Collection;

/**
 * Configuration set. A configuration set groups a collection of specific configuration sets logically.
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 17:45 27.07.11
 *
 * @author Burt Parkers
 * @version 1.0
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "ConfigSet.findByName",
                query = "select configSet from ConfigSet configSet where configSet.name = ?1")
})
public class ConfigSet {

    /** The id field. */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    /** Name of the config set. */
    @Column(nullable = false, length = 50, unique = true)
    private String name;

    /** A collection of aggregated config attributes. */
    @OneToMany(cascade = {CascadeType.ALL})
    private Collection<ConfigAttribute> configAttributes;

    /**
     * Returns the id.
     *
     * @return the id.
     */
    public int getId() {
        return id;
    }

    /**
     * Sets the id.
     *
     * @param id the id.
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * Returns the name.
     *
     * @return the name;
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name.
     *
     * @param name the name.
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Returns the config attributes.
     *
     * @return the config attributes.
     */
    public Collection<ConfigAttribute> getConfigAttributes() {
        return configAttributes;
    }

    /**
     * Sets the config attributes.
     *
     * @param configAttributes the config attributes.
     */
    public void setConfigAttributes(Collection<ConfigAttribute> configAttributes) {
        this.configAttributes = configAttributes;
    }

    /**
     * Returns a config attribute, identified by its name.
     *
     * @param name name of the config set.
     * @return the config set or null if not found.
     */
    public ConfigAttribute getConfigAttribute(String name) {
        ConfigAttribute configAttribute = null;
        for (ConfigAttribute attribute : configAttributes) {
            if (attribute.getName().equals(name)) {
                configAttribute = attribute;
                break;
            }
        }
        return configAttribute;
    }
}
